import request from '@/utils/request';

/**
 * 分页查询对讲机账号
 */
export async function pageDevices(params) {
  const res = await request.get('/audio/device/page', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询对讲机账号列表
 */
export async function listDevices(params) {
  const res = await request.get('/audio/device', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询对讲机账号
 */
export async function getDevice(id) {
  const res = await request.get('/audio/device/' + id);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加对讲机账号
 */
export async function addDevice(data) {
  const res = await request.post('/audio/device', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改对讲机账号
 */
export async function updateDevice(data) {
  const res = await request.put('/audio/device', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除对讲机账号
 */
export async function removeDevice(id) {
  const res = await request.delete('/audio/device/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 从对讲组中移除对讲账号
 */
export async function removeDeviceFromGroup(deviceId,groupId) {
  const res = await request.delete('/audio/device/removeFromGroup/' + deviceId + '/' + groupId);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}



/**
 * 导入对讲机账号
 */
export async function importDevices(file) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post('/audio/device/import', formData);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}


